/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @author 卢清波
 * @date 2021/1/25 12:52
 */
public class NettyServer
{
    public static void main(String[] args) throws InterruptedException
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup(10);
        try
        {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup);
            serverBootstrap.option(ChannelOption.SO_BACKLOG,1024);
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>()
            {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception
                {
                    socketChannel.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
                    socketChannel.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
                    socketChannel.pipeline().addLast(new NettyServerHandler());
                }
            });
            System.out.println("服务端开始启动");
            ChannelFuture channelFuture = serverBootstrap.bind(9000).sync();
            channelFuture.addListener(new ChannelFutureListener()
            {
                public void operationComplete(ChannelFuture channelFuture) throws Exception
                {
                    if (channelFuture.isSuccess())
                    {
                        System.out.println("监听端口9000成功");
                    }else{
                        System.out.println("监听端口9000失败");
                    }

                }
            });

            ChannelFuture sync = channelFuture.channel().closeFuture().sync();

            sync.addListener(new ChannelFutureListener()
            {
                public void operationComplete(ChannelFuture channelFuture) throws Exception
                {
                    if (channelFuture.isDone())
                    {
                        System.out.println("监听通道关闭");
                    }
                }
            });
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
        }finally
        {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }


    }
}
